// import {getUsers,getUser,} from "../service/user";
// import { set } from "vue/types/umd";
import { getItems, getItem, getCartOrder } from "../service/cart";
import { getSession } from "../service/user";
export default {
  namespaced: true,
  state: {
    items: [],
    //用户编号
    userSerial: "",
    user:{},
    item: {},
    //添加、删除传输的对象
    cartOrder: {
      item: {},
      buyNum: "",
      userId: "",
      typeCode: "",
    },
    //订单集合
    cartOrders: [],
    totalPrice: "",
    searchParams: {
      type: "",
      value: "",
      page: "",
      size: "",
    },
    pagination: {},
  },
  mutations: {
    setTotalPrice(state, totalPrice) {
      state.totalPrice = totalPrice;
    },
    setUserSerial(state, userSerial) {
      state.userSerial = userSerial;
    },
    setUser(state, user) {
      state.user = user;
    },
    setItems(state, items) {
      state.items = items;
    },
    setCartOrders(state, cartOrders) {
      state.cartOrders = cartOrders;
    },
    setPagination(state, paginationObject) {
      state.pagination = paginationObject;
    },
    setItem(state, item) {
      state.item = item;
    },
  },
  actions: {
    async setItemsTable(context, pageParam = {}) {
      if (pageParam.type) {
        context.state.searchParams.type = pageParam.type;
        context.state.searchParams.value = pageParam.value;
      } else {
        pageParam.type = context.state.searchParams.type;
        pageParam.value = context.state.searchParams.value;
      }

      let response = await getItems(pageParam);
      console.log(response)
      context.commit("setItems", response.data.data.list);
      context.commit("setPagination", response.data.data);
    },
    async getCartOrders(context, userId) {
      let response = await getCartOrder(userId);
      context.commit("setCartOrders", response.data.data);
      // var price=0;
      // for (let index = 0; index < response.data.data.length; index++) {
      //    price+=response.data.data.itemPrice*response.data.data.buyNum;

      // }
      // context.commit("setTotalPrice", price);
    },
    async setUser(context) {
      let response = await getSession();
      context.commit("setUserSerial", response.data.userSerial);
      context.commit("setUser", response.data);
    },
    async setItem({ commit }, itemId) {
      getItem(itemId).then(function(response) {
        let item = response.data.data;
        commit("setItem", item);
      });
    },
  },
};
